import { LOGIN,ACTIVITY_INFO_CHANGE,SET_MY_ACTIVITY_LIST } from '../constants';
import { parseJSON,setCookie,getCookie,token } from '../utils/';
import { Toast } from 'antd-mobile';

export function loginSuccess(data) {
  return {
    type: LOGIN['SUCCESS'],
    payload: data
  }
}

export function setMyActivityList(data){
  return {
    type:SET_MY_ACTIVITY_LIST,
    payload:data
  }
}

export function valueChange(t,e){
  return {
    type:ACTIVITY_INFO_CHANGE,
    payload:{
      type:t,
      e:e,
    }
  }
}

/**
 * 获取用户信息
 */
export function getUserInfo(){
  return (dispatch, state) => {
    Toast.info('加载中...',0);
    return fetch(`/api/User/getUserInfo?token=`+getCookie('token'), {
      method:'get',
      headers: {
        Accept:'application/json',
        'Content-Type': 'application/json;charset=UTF-8'
      }
        })
        .then(parseJSON)
        .then(res => {
          if(res.code == 200){
            dispatch(loginSuccess(res.data))
            Toast.hide(); 
          }else{
            Toast.hide();
            Toast.info(res.msg,2);
            location.replace("#/");
          }
        })
        .catch(error => {
            Toast.hide();
            Toast.info('服务器出错啦~');
        })
  }
}

/**
 * 获取活动详情
 * @param {*} id 
 */
export function getActivityDetail(id,callback){
  return (dispatch, state) => {
    Toast.info('加载中...',0);
    return fetch(`/api/Activity/detail?id=${id}`+'&token='+getCookie('token'), {
      method:'get',
      headers: {
        Accept:'application/json',
        'Content-Type': 'application/json;charset=UTF-8'
      }
        })
        .then(parseJSON)
        .then(res => {
          if(res.code == 200){
            Toast.hide(); 
            dispatch(valueChange('all',JSON.parse(res.data.content)))
            callback();
          }else{
            Toast.hide();
            Toast.info(res.msg,2);
          }
        })
        .catch(error => {
            Toast.hide();
            Toast.info('服务器出错啦~');
        })
  }
}

/**
 * 获取我的活动列表
 */
export function getMyActivityList(){
  return (dispatch, state) => {
    Toast.info('加载中...',0);
    return fetch(`/api/Activity/myList?token=`+getCookie('token')+'&uid='+getCookie('uid'), {
      method:'get',
      headers: {
        Accept:'application/json',
        'Content-Type': 'application/json;charset=UTF-8'
      }
        })
        .then(parseJSON)
        .then(res => {
          if(res.code == 200){
            Toast.hide(); 
            dispatch(setMyActivityList(res.data))
          }else{
            Toast.hide();
            Toast.info(res.msg,2);
          }
        })
        .catch(error => {
            Toast.hide();
            Toast.info('服务器出错啦~');
        })
  }
}

/**
 * 退出登录
 */
export function logout(){
  return (dispatch, state) => {
    Toast.info('加载中...',0);
    return fetch(`/api/login/out?token=`+getCookie('token'), {
      method:'get',
      headers: {
        Accept:'application/json',
        'Content-Type': 'application/json;charset=UTF-8'
      }
        })
        .then(parseJSON)
        .then(res => {
          if(res.code == 200){
            Toast.hide(); 
            setCookie('token','',0);
            setCookie('uid','',0);
            Toast.info(res.msg,2);
            location.replace("#/");
          }else{
            Toast.hide();
            Toast.info(res.msg,2);
          }
        })
        .catch(error => {
            Toast.hide();
            Toast.info('服务器出错啦~');
        })
  }
}

/**
 * 登录
 * @param {*} query 
 */
export function login(query){
    return (dispatch, state) => {
      Toast.info('登录中...',0);
      return fetch(`/api/login/`, {
        method:'post',
        body:JSON.stringify(query),
        headers: {
          Accept:'application/json',
          'Content-Type': 'application/json;charset=UTF-8'
        }
          })
          .then(parseJSON)
          .then(res => {
            if(res.code == 200){
              dispatch(loginSuccess(res.data))
              Toast.hide();
              setCookie('token',res.data.token,30);
              setCookie('uid',res.data.id,30);
              //Toast.info(res.msg,2);
              location.replace("#/Home");
            }else{
              Toast.hide();
              Toast.info(res.msg,2);
            }
          })
          .catch(error => {
            Toast.hide();
            Toast.info('服务器出错啦~');
          })
    }
}

/**
 * 新增活动
 * @param {*} query 
 */
export function addActivity(query){
  return (dispatch, state) => {
    Toast.info('保存中...',0);
    return fetch(`/api/activity/add?token=${getCookie('token')}`, {
      method:'post',
      body:JSON.stringify(query),
      headers: {
        Accept:'application/json',
        'Content-Type': 'application/json;charset=UTF-8'
      }
        })
        .then(parseJSON)
        .then( (res) => {
          if(res.code == 200){
            Toast.info(res.msg,2);
            dispatch(valueChange('id',res.data.id));
          }else{
            Toast.info(res.msg,2);
          }
        })
        .catch(error => {
          Toast.hide();
          Toast.info('服务器出错啦~');
        })
  }
}

/**
 * 注册用户
 * @param {*} query 
 */
export function register(query){
  return (dispatch, state) => {
    Toast.info('注册中...',0);
    return fetch(`/api/login/add`, {
      method:'post',
      body:JSON.stringify(query),
      headers: {
        Accept:'application/json',
        'Content-Type': 'application/json;charset=UTF-8'
      }
        })
        .then(parseJSON)
        .then( (res) => {
          if(res.code == 200){
            dispatch(loginSuccess(res.data));
            setCookie('token',res.data.token,30);
            setCookie('uid',res.data.id,30);
            Toast.info('注册成功~已自动登录',2);
            location.replace("#/Home");
          }else{
            Toast.info(res.msg,2);
          }
        })
        .catch(error => {
          Toast.hide();
          Toast.info('服务器出错啦~');
        })
  }
}